package com.starbaba.core.utils;

import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ehcache工具类
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2013-10-25 下午02:37:55
 * @version V1.0
 */
@SuppressWarnings("unchecked")
public class EhcacheUtil {
	
	private static Logger log = LoggerFactory.getLogger(EhcacheUtil.class);
	
	protected static volatile CacheManager cacheManager = null;
	
	/**
	 * 获取ehcache管理器
	 * @param ehcache 
	 * @return CacheManager
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午02:44:51
	 * @version V1.0
	 */
	public static CacheManager getCacheManager(String configFileName) {
		if(cacheManager == null) {
			synchronized (EhcacheUtil.class) {
				if(cacheManager == null) {
					if(StringUtils.isNotBlank(configFileName)) {
						cacheManager = CacheManager.create(configFileName);
					} else {
						cacheManager = CacheManager.create();
					}
				}
			}
		}
		
		return cacheManager;
	}
	
	/**
	 * 获取ehcache管理器
	 * @return CacheManager
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午02:44:51
	 * @version V1.0
	 */
	public static CacheManager getCacheManager() {
		return getCacheManager(null);
	}
	
	/**
	 * 获取在ehcache.xml中配置的cache，或者临时创建一个cache
	 * @param cacheName cache名称
	 * @return Cache cache实例
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2014-12-5 下午05:36:01
	 * @version V1.0
	 */
	protected static Cache getOrAddCache(String cacheName) {
		CacheManager cm = getCacheManager();
		Cache cache = cm.getCache(cacheName);
		if (cache == null) {
			synchronized(cm) {
				cache = cm.getCache(cacheName);
				if (cache == null) {
					log.warn("Could not find cache config [" + cacheName + "], using default.");
					cm.addCacheIfAbsent(cacheName);
					cache = cm.getCache(cacheName);
					log.debug("Cache [" + cacheName + "] started.");
				}
			}
		}
		return cache;
	}
	
	public static void put(String cacheName, Object key, Object value) {
		getOrAddCache(cacheName).put(new Element(key, value));
	}
	
	public static <T> T get(String cacheName, Object key) {
		Element element = getOrAddCache(cacheName).get(key);
		return element != null ? (T)element.getObjectValue() : null;
	}
	
	public static List<?> getKeys(String cacheName) {
		return getOrAddCache(cacheName).getKeys();
	}
	
	public static void remove(String cacheName, Object key) {
		getOrAddCache(cacheName).remove(key);
	}
	
	public static void removeAll(String cacheName) {
		getOrAddCache(cacheName).removeAll();
	}
	
	public static void main(String[] args) {
		String value = EhcacheUtil.get("cacheName", "key");
		System.out.println("value=" + value);
	}
}
